<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <div class="filter-wrapper">
      <NewPageFilter
        v-model:query="filterInfo.query.queryMap"
        :query-more-json="true"
        :filter-list="filterInfo.searchList"
        :filter-btn="filterInfo.searchBtn"
        :clear-type="clearType"
        :slip-select-request="slipSelectRequest"
      >
      </NewPageFilter>
      <div class="display-flex">
        <el-button @click="preferenceInfo.visible = true">偏好设置</el-button>
      </div>
    </div>
    <!-- 条件栏 -->
    <DialogTable
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      :api="tableInfo.api"
      :query="filterInfo.query"
      v-model:data="tableInfo.data"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :isDynamic="false"
      @handleEvent="handleEvent"
    >
    </DialogTable>
  </page-dialog>
  <SetPreference
    v-if="preferenceInfo.visible"
    v-model:visible="preferenceInfo.visible"
    :search-list="filterInfo.searchList"
    :field-list="tableInfo.fieldList"
    :customize-user-v-o="filterInfo.query.customizeUserVO"
    @callback="setPreferenceCallback"
  />
</template>
<script setup name="selectReceivingSupplier">
const SetPreference = defineAsyncComponent(() =>
  import('@/components/SetPreference/defaultSetPreference')
)
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'
import { getsupplierscustomizeListPages } from '@/api/core.js'

const { proxy } = getCurrentInstance()
const emit = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  visible: {
    type: Boolean,
    default: false,
  },
})

const shows = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emit('update:visible', newValue) // 更新父组件shows
  },
})

const clearType = ref(true)
const selectRow = ref({})
const slipSelectRequest = ref(true)
const filterInfo = reactive({
  code: '',
  query: {
    copyQueryMap: {},
    queryMap: {},
    queryParams: {},
    customizeUserVO: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})

// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: '',
  fieldList: [],
})
// 弹窗相关
const dialogInfo = reactive({
  title: '选择供应商',
  width: '80%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    { label: '保存', event: handleSave, disabled: true, show: true },
  ],
})
const preferenceInfo = ref({
  visible: false,
})

setData()
function setData() {
  let obj = {
    supplierName: {
      label: '供应商名称',
      value: 'supplierName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
  }
  tableInfo.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function getCustomize(_slipSelectRequest = true) {
  let param = {
    tableKey:
      'receivingOrderSupplierQuery-com.slip.spd.entity.vo.core.CoreSupplierVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.query.customizeUserVO = data
      tableInfo.fieldList = proxy.$fn.setFieldList(
        tableInfo.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.searchList,
        data.detail
      )
      const tempMap = {
        supplierAuth: {
          asTbName: 'core_supplier',
          columnName: 'supplier_id',
          queryType: 'IN',
          queryCategory: 'AND',
          value: 'supplierAuth',
        },
      }
      filterInfo.query.queryMap = {
        ...searchInfo.queryMap,
        ...tempMap,
      }
      filterInfo.searchList = searchInfo.searchList
      filterInfo.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.copyQueryMap = proxy.$fn.deepClone(filterInfo.query.queryMap)
      slipSelectRequest.value = _slipSelectRequest
      tableInfo.api = getsupplierscustomizeListPages
      getList()
    }
  })
}
function getList() {
  proxy.$nextTick(() => {
    tableInfo.refresh = Math.random()
  })
}
function search() {
  proxy.$nextTick(() => {
    tableInfo.initCurpage = Math.random()
    tableInfo.refresh = Math.random()
  })
}
function handleEvent(event, data) {
  if (event === 'tableClickRow') {
    if (data && data.id) {
      selectRow.value = data
      dialogInfo.btList[1].disabled = false
    }
  }
}
function handleSave() {
  shows.value = false
  emit('callback', selectRow.value)
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
}
function clearSearch() {
  filterInfo.query.queryMap = proxy.$fn.deepClone(filterInfo.copyQueryMap)
  clearType.value = !clearType.value
  getList()
}
function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}
</script>
